package com.qunar.paydata.platform.common.util;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpSession;

/**
 * 
 * @author leonyang.liu
 *
 */
public class EmailUtil {
	public static void createEmail(String chart_comment,String random_1,String random_2,HttpSession hSession) throws Exception {
		Properties props = new Properties(); // 用于连接邮件服务器的参数配置（发送邮件时才需要用到）
		props.setProperty("mail.smtp.host", "mail.corp.qunar.com");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props); // 根据参数配置，创建会话对象（为了发送邮件准备的）
//		session.setDebug(true);
		Transport ts = session.getTransport();
		//发件人信息
		ts.connect("mail.corp.qunar.com", "paydata", "qunar2017W");
		MimeMessage message = new MimeMessage(session); // 创建邮件对象

		// 2.发件人
		message.setFrom(new InternetAddress("paydata@qunar.com",
				"错误工单中心", "UTF-8"));

		// 3. To: 收件人信息
//		message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
//				"chuanyang.yu@qunar.com", "chuanyang.yu", "UTF-8"));
		InternetAddress[] addresses = new InternetAddress[]{new InternetAddress(
					"leonyang.liu@qunar.com", "leonyang.liu", "UTF-8"),
					new InternetAddress("wenchao.e@qunar.com", "wenchao.e", "UTF-8"),			
					new InternetAddress("ruihao.li@qunar.com", "ruihao.li", "UTF-8"),
		 			new InternetAddress("qq.zhao@qunar.com", "qq.zhao", "UTF-8"),
		 			new InternetAddress("jinluo.wang@qunar.com", "jinluo.wang", "UTF-8"),
		 			new InternetAddress("chengbo.li@qunar.com", "chengbo.li", "UTF-8")
		 };

		message.setRecipients(MimeMessage.RecipientType.TO, addresses);
		// Cc: 抄送（可选）
		// message.setRecipient(MimeMessage.RecipientType.CC, new
		// InternetAddress("leonyang.liu@qunar.com", "USER_EE", "UTF-8"));

		// Bcc: 密送（可选）
		// message.setRecipient(MimeMessage.RecipientType.BCC, new
		// InternetAddress("leonyang.liu@qunar.com", "USER_FF", "UTF-8"));

		// 4. Subject: 邮件主题
		message.setSubject("工单报表", "UTF-8");

		// // 5. Content: 邮件正文（可以使用html标签）
		// MimeBodyPart text = new MimeBodyPart();
		// text.setContent("这是一封带 图片的邮件：<img src='cid:xxx.jpg'>","text/html;charset=UTF-8");
		// message.setContent("TEST这是邮件正文。。。", "text/html;charset=UTF-8");
		//
		// // 6. 设置显示的发件时间
		// message.setSentDate(new Date());

		//第一张图片
		MimeBodyPart text = new MimeBodyPart();
		
		text.setContent("<h1><small><i>备注说明："+chart_comment+"</i></small></h1><h1><img src='cid:chart'></h1>"
				+ "<h1><img src='cid:ws_num'></h1>","text/html;charset=UTF-8");
		
		// 准备图片数据
		MimeBodyPart image = new MimeBodyPart();
		DataHandler dh = new DataHandler(new FileDataSource(hSession.getServletContext().getRealPath("/")+"images/chart/chart_"+random_1+".jpeg"));
		image.setDataHandler(dh);
		image.setContentID("chart");
		
		//第二张图片
		MimeBodyPart image_1 = new MimeBodyPart();
		DataHandler dh1 = new DataHandler(new FileDataSource(hSession.getServletContext().getRealPath("/")+"images/chart/ws_num_"+random_2+".jpeg"));
		image_1.setDataHandler(dh1);
		image_1.setContentID("ws_num");
		
		// 描述数据关系
		MimeMultipart mm = new MimeMultipart();
		mm.addBodyPart(text);
		mm.addBodyPart(image);
		mm.addBodyPart(image_1);
		mm.setSubType("related");
		message.setContent(mm);
				
		// 7. 保存前面的设置
		message.saveChanges();

		ts.sendMessage(message, message.getAllRecipients());
		ts.close();

		// 8. 将该邮件保存到本地
		OutputStream out = new FileOutputStream(hSession.getServletContext().getRealPath("/")+"/email/myEmail.eml");
		message.writeTo(out);
		out.flush();
		out.close();
	}
}
